<template>

    <button @click="changeNode">修改节点颜色事件</button>

    <h2>动态 连接箭头</h2>

    <div id="chart-diagram" style="background-color: whitesmoke; width: 1000px; height: 1000px;">
    </div>


</template>

<script setup>
    import {onMounted} from 'vue';
    import go from "gojs";

    let myDiagram = null;


    const changeNode = () => {
        console.log("===========")
        var data = myDiagram.model.findNodeDataForKey("Alpha");
        console.log(myDiagram.model)
        console.log(data)
        // 这个方式可以更新
        if (data !== null) myDiagram.model.setDataProperty(data, "color", "red");
    }

    const init = () => {

        const $ = go.GraphObject.make

        myDiagram = new go.Diagram("chart-diagram", {
            'grid.visible': true, // 显示网格
            'clickCreatingTool.archetypeNodeData': {category: 'Normal', text: '新节点', notice: ''}, // 双击新建节点(可以写入节点的默认信息)；；
            'toolManager.hoverDelay': 100, // tooltip提示显示延时
            'toolManager.toolTipDuration': 10000, // tooltip持续显示时间
            allowMove: true, // 允许拖动画板
            allowDragOut: true, // 允许拖拽节点
            allowDelete: true, // 允许删除节点
            allowCopy: true, // 允许复制节点
            allowClipboard: true, // 允许粘贴节点

        });

        myDiagram.nodeTemplate =
            $(go.Node, "Auto",
                {locationSpot: go.Spot.Center,selectionAdorned: false},

                $(go.Shape, "RoundedRectangle",
                    {stroke: "orange", strokeWidth: 2},new go.Binding("fill", "color")),
                $(go.TextBlock,
                    {margin: 5},
                    new go.Binding("text", "key"))
            );

        var nodeDataArray = [
            {key: "Alpha",color:'orange'},
            {key: "Beta",color:'green'},
            {key: "Gamma",color:'blue'}
        ];
        var linkDataArray = [
            {from: "Alpha", to: "Beta"}
        ];
        myDiagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

        function switchTo() {
            // 所有的操作都在方法内执行
            myDiagram.model.commit(function (m) {


                var data = m.linkDataArray[0];  // 获取第一个linkDataArray得node值
                if (m.getToKeyForLinkData(data) === "Beta")
                    m.setToKeyForLinkData(data, "Gamma");
                else
                    m.setToKeyForLinkData(data, "Beta");
            }, "reconnect link");
        }

        function loop() {
            setTimeout(function () {
                switchTo();
               loop();
            }, 1000);
        }

        loop();


    };

    onMounted(() => {
        init();
    });


</script>

<style scoped>

</style>